package common.library.utils;

import com.fasterxml.jackson.databind.ObjectMapper;

import org.json.JSONArray;
import org.json.JSONObject;

public class JsonUtil {

    static final ObjectMapper objectMapper = new ObjectMapper();

    public static final JSONObject parse(String json) throws Exception {
        return new JSONObject(json);
    }

    public static final String serialization(Object value) {
        try {
            return objectMapper.writeValueAsString(value);
        } catch (Exception ex) {
            return ex.getMessage();
        }
    }

    public static JSONObject parse(Object value) throws Exception {
        return new JSONObject(serialization(value));
    }

    public static <T> T deserialization(Class<T> tClass, String json) throws Exception {
        return objectMapper.readValue(json, tClass);
    }

    public static <T> T deserialization(Class<T> tClass, JSONObject jsonObject) throws Exception {
        return deserialization(tClass, jsonObject.toString());
    }

    public static <T> T deserialization(Class<T> tClass, JSONArray jsonArray) throws Exception {
        return deserialization(tClass, jsonArray.toString());
    }
}
